package com.itheima.service.store.company.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.company.CompanyDao;
import com.itheima.domain.store.company.Company;
import com.itheima.factory.MapperFactory;
import com.itheima.service.store.company.CompanyService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class CompanyServiceImpl implements CompanyService {


    @Override
    public List<Company> findAll() {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        CompanyDao mapper = MapperFactory.getMapper(sqlSession, CompanyDao.class);
        List<Company> all = mapper.findAll();
        TransactionUtil.close(sqlSession);

        return all;
    }

    /*
       查询所有--分页显示
       所有不是整个信息库的list集合，是当前页面想要显示的部分信息集合
       */
    @Override
    public PageInfo findAll(int currentPage,int pageSize) {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        CompanyDao mapper = MapperFactory.getMapper(sqlSession, CompanyDao.class);
         PageHelper.startPage(currentPage, pageSize);
        List<Company> all = mapper.findAll();
        PageInfo pageInfo =new PageInfo(all);
        TransactionUtil.close(sqlSession);
        return pageInfo;
    }

    /*
      查询单个
       */
    @Override
    public Company findById(String id) {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        CompanyDao mapper = MapperFactory.getMapper(sqlSession, CompanyDao.class);
        Company company = mapper.findById(id);
        TransactionUtil.close(sqlSession);
        return company;
    }

    /*
    添加
     */
    @Override
    public int save(Company company) {
        int rst=0;
        SqlSession sqlSession =null;
       try{
            sqlSession = MapperFactory.getSqlSession();
           CompanyDao mapper = MapperFactory.getMapper(sqlSession, CompanyDao.class);
           //UUID生成id
           String id = UUID.randomUUID().toString();
           company.setId(id);
           rst = mapper.save(company);
           TransactionUtil.commit(sqlSession);
           this.findAll();
       }catch (Exception e){
           e.printStackTrace();
       }finally {
           TransactionUtil.close(sqlSession);
       }
       return rst;
    }

    /*
    修改
     */
    @Override
    public int update(Company company) {

        SqlSession sqlSession = MapperFactory.getSqlSession();
        CompanyDao mapper = MapperFactory.getMapper(sqlSession, CompanyDao.class);
        int rst = mapper.update(company);
        System.out.println(rst);
        TransactionUtil.commit(sqlSession);
        this.findAll();
        TransactionUtil.close(sqlSession);
        return rst;
    }

    /*
    删除
     */
    @Override
    public int delete(Company company) {

        SqlSession sqlSession = MapperFactory.getSqlSession();
        CompanyDao mapper = MapperFactory.getMapper(sqlSession, CompanyDao.class);
        int rst = mapper.delete(company);
        TransactionUtil.commit(sqlSession);
        this.findAll();
        TransactionUtil.close(sqlSession);
        return rst;
    }
}
